package com.shiguiwu.springboot3.controller;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.shiguiwu.springboot3.dto.UserDTO;
import com.shiguiwu.springboot3.feign.DynamicUrlInterceptor;
import com.shiguiwu.springboot3.feign.GenOpenFeignFactory;
import com.shiguiwu.springboot3.feign.UserFeignCustomConfigClient;
import com.shiguiwu.springboot3.feign.UserFeignDynamicUriClient;
import com.shiguiwu.springboot3.feign.UserFeignDynamicUrlClient;
import com.shiguiwu.springboot3.feign.UserFeignUrlClient;
import com.shiguiwu.springboot3.feign.UserFeignUrlConfigClient;
import com.shiguiwu.springboot3.feign.UserFeignUrlPathClient;
import com.shiguiwu.springboot3.service.OrderService;
import com.shiguiwu.springboot3.util.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @description: 订单控制层
 * @author: stone
 * @date: Created by 2023/7/3 12:10
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.controller
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserFeignUrlClient userFeignUrlClient;

    @Autowired
    private UserFeignUrlPathClient userFeignUrlPathClient;

    @Autowired
    private UserFeignUrlConfigClient userFeignUrlConfigClient;

    @Autowired
    private UserFeignCustomConfigClient userFeignCustomConfigClient;

    @Autowired
    private GenOpenFeignFactory<UserFeignDynamicUrlClient> genOpenFeignFactory;
    @Autowired
    private GenOpenFeignFactory<UserFeignDynamicUriClient> openFeignFactory;

    @GetMapping("/test")
    public R<Integer> test(HttpServletRequest request) {
        return R.ok(orderService.test());
    }


    @GetMapping("/test2")
    public R<Integer> test2(HttpServletRequest request) {
        return R.ok(orderService.test2());
    }


    @GetMapping("/test3")
    public R<Integer> test3(HttpServletRequest request) {
        return R.ok(orderService.test3());
    }

    @GetMapping("/test4")
    public R<UserDTO> test4(HttpServletRequest request) {
        return orderService.test4();
    }

    @GetMapping("/test5")
    public R<UserDTO> test5(HttpServletRequest request) {
        return orderService.test5();
    }


    /**
     * 测试openfeign超时
     * @param request
     * @param time
     * @return
     */
    @GetMapping("/openfeign/timeout")
    public R<UserDTO> timeout(HttpServletRequest request,long time) {
        return orderService.timeout(time);
    }


    /**
     * 测试openfeign超时
     * @param request
     * @param time
     * @return
     */
    @GetMapping("/openfeign/ex")
    public R<Integer> ex(HttpServletRequest request,long time) {
        return orderService.ex(time);
    }

    /**
     * openFeign测试url
     * @return
     */
    @GetMapping("/test/url")
    public R<Integer> testUrl() {
        return userFeignUrlClient.test();
    }

    /**
     * openFeign测试url加上path
     * @return
     */
    @GetMapping("/test/url/path")
    public R<Integer> testUrlPath() {
        return userFeignUrlPathClient.test();
    }

    /**
     * openFeign测试自带的配置的url
     * @return
     */
    @GetMapping("/test/url/config")
    public R<Integer> testUrlConfig() {
        return userFeignUrlConfigClient.test();
    }

    /**
     * openFeign定制的url
     * @return
     */
    @GetMapping("/test/custom/config")
    public R<Integer> testUrCustomConfig() {
        return userFeignCustomConfigClient.test();
    }

    /**
     * openFeign 的动态的url方式1
     * @return
     */
    @GetMapping("/test/dynamic/url")
    public R<Integer> testUrDynamicUrl() {
        return genOpenFeignFactory
                .genFeignDynamicUrlClient(() -> new DynamicUrlInterceptor
                                .OpenFeignDTO("http://127.0.0.1:8095", true)
                        , UserFeignDynamicUrlClient.class)
                .test();
    }


    /**
     * openFeign 的动态的url方式2
     * @return
     */
    @GetMapping("/test/dynamic/url/v2")
    public R<Integer> testUrDynamicUrlV2() {
        UserFeignDynamicUriClient client = openFeignFactory
                .genFeignDynamicUrlClientV2(UserFeignDynamicUriClient.class);
        return client.test(URI.create("http://127.0.0.1:8095/user/test2"));
    }

    public static void main(String[] args) throws NacosException, InterruptedException {
        String serverAddr = "192.162.11.191:8845";
        String dataId = "service.vgroupMapping.default-tx-group.v2";//service.vgroupMapping.default_tx_group
        String group = "SEATA_GROUP";//SEATA_GROUP
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        properties.put("password", "nacos");
        properties.put("username", "nacos");
        properties.put("isUseEndpointParsingRule", "false");
        properties.put("isUseCloudNamespaceParsing", "false");
        //properties.put("namespace", "public");
        ConfigService configService = NacosFactory.createConfigService(properties);
        String content = configService.getConfig(dataId, group, 5000);
        log.info("拿到的返回结果==> {}", content);
        configService.addListener(dataId, group, new Listener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                log.info("recieve:" + configInfo);
            }

            @Override
            public Executor getExecutor() {
                return null;
            }
        });

        boolean isPublishOk = configService.publishConfig(dataId, group, "default");
        log.info("是否发布 ===> {}", isPublishOk);

        Thread.sleep(3000);
        content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);

        boolean isRemoveOk = configService.removeConfig(dataId, group);
        System.out.println(isRemoveOk);
        log.info("是否移除 ===> {}", isRemoveOk);

        Thread.sleep(3000);

        content = configService.getConfig(dataId, group, 5000);
        log.info("拿到的返回结果2==> {}", content);
        Thread.sleep(300000);

    }

}
